/**
 * 1
 *  
 * @Author : Ruoyu Wang
 * @DateTime : 24-10-15 下午6:54
 */

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        long long n, k;
        cin >> n >> k;
        if (k <= (n + 1) / 2) {
            cout << k << endl;
        } else {
            cout << n - k + 1 << endl;
        }
    }
    return 0;
}